package cn.chinaflame.pagodas.web;

import cn.chinaflame.pagodas.Result.Result;
import cn.chinaflame.pagodas.annotation.ApiDoc;
import cn.chinaflame.pagodas.dto.MemberRegistDto;
import cn.chinaflame.pagodas.dto.PaMemberDto;
import cn.chinaflame.pagodas.enums.CodeEnum;
import cn.chinaflame.pagodas.repository.PaMemberRepository;
import cn.chinaflame.pagodas.req.*;
import cn.chinaflame.pagodas.service.CommonService;
import cn.chinaflame.pagodas.service.PaMemberService;
import cn.chinaflame.pagodas.service.PaMemberServiceNew;
import cn.chinaflame.pagodas.utils.ParamPreconditions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import cn.chinaflame.pagodas.Result.Result;
import cn.chinaflame.pagodas.entity.Boy;
import cn.chinaflame.pagodas.repository.BoyRepository;
import cn.chinaflame.pagodas.service.BoyService;
import cn.chinaflame.pagodas.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
/**
 * 会员相关接口
 *
 * @author admin
 */
@RestController
@Transactional(rollbackFor = Exception.class)
@RequestMapping(value = "/openapi/v1/pagodas/member/")
public class OpenPaMemberAPI {
    private static final Logger log = LoggerFactory.getLogger(OpenPaMemberAPI.class);
    @Autowired
    private PaMemberService memberService;
    @Autowired
    private PaMemberRepository ywMemberRepository;
    @Autowired
    private CommonService commonService;
    @Autowired
    private PaMemberServiceNew paMemberServiceNew;

    /**
     * 会员注册接口
     *
     * @param req
     * @return
     */
    @PostMapping("regist")
    @ApiDoc(RegistReq.class)
    public Object regist(@RequestBody RegistReq req) {
        log.info("佛龛会员注册req:{}", req);
        MemberRegistDto dto = memberService.regist(req.getMobile(), req.getLoginId(),
                req.getLoginPwd(), req.getEmail());
        return Result.create(dto);
    }

    /**
     * 会员登录
     *
     * @param req
     * @return
     */
    @PostMapping("login")
    @ApiDoc(RegistReq.class)
    public Object login(@RequestBody LoginReq req) {
        log.info("佛龛会员登录req:{}", req);
        MemberRegistDto dto = memberService.login(req.getUsername(), req.getPassword());
        return Result.create(dto);
    }
	
	
	/*
	@PostMapping(value="query/list")
	public Object findALLMembers(@RequestBody PaMemberReq req){
		log.info("会员列表查询req:{}", req);	
		if(commonService.paginationSupport(req.getPageIndex(), req.getPageSize())) {
			return memberService.queryByPage(req);
		}			
		return memberService.query(req);
	}*/
//	/**
//	 * 
//	 * @param req
//	 * @return
//	 */
//	@PostMapping(value="query/list")
//	public Object findALLMembers(@RequestBody PaMemberReq req){
//		log.info("会员列表查询req:{}", req);	
//		if(commonService.paginationSupport(req.getPageIndex(), req.getPageSize())) {
//			return memberService.queryByPage(req);
//		}			
//		return memberService.query(req);
//	
//	}

    /**
     * 新的客户列表接口
     *
     * @param req
     * @return
     */
    @PostMapping(value = "query/list/new")
    @ApiDoc(BaseReq.class)
    public Object findAllmembersNew(@RequestBody BaseReq req) {
        log.info("会员列表查询req:{}", req);
        if (commonService.paginationSupport(req.getPageIndex(), req.getPageSize())) {
            return paMemberServiceNew.queryByPage(req);
        }
        return Result.create("ERROR", "少参数pageIndex或者pageSize");
    }


    /**
     * 查询单个客户信息
     *
     * @param id 客户id
     * @return
     */
    @GetMapping(value = "query/{id}")
    @ApiDoc(Result.class)
    public Object findMember(@PathVariable String id) {
        ParamPreconditions.checkNotEmpty(id, CodeEnum.FAIL.getCode(), "id为空");
        PaMemberDto dto = memberService.findOne(id);
        ParamPreconditions.checkNotNull(dto != null, CodeEnum.FAIL.getCode(), "没有这个客户");
        return Result.create(dto);
    }

    /**
     * 添加单个客户
     *
     * @return
     */
    @PostMapping(value = "add")
    @ApiDoc(PaMemberReq.class)
    public Object addMember(@RequestBody PaMemberReq req) {
        log.info("添加会员req:{}", req);
        PaMemberDto dto = new PaMemberDto();
        BeanUtils.copyProperties(req, dto);
        memberService.add(dto);
        return Result.create(dto);
    }

    /**
     * 会员信息修改
     *
     * @return
     */
    @PutMapping(value = "update")
    @ApiDoc(PaMemberReq.class)
    public Object update(@RequestBody PaMemberReq req) {
        log.info("更新会员req:{}", req);
        PaMemberDto dto = new PaMemberDto();
        BeanUtils.copyProperties(req, dto);
        memberService.update(dto);
        return Result.create(dto);
    }

    /**
     * 删除客户
     *
     * @param id 客户id
     * @return
     */
    @DeleteMapping(value = "delete/{id}")
    @ApiDoc(Result.class)
    public Object deleteMember(@PathVariable("id") String id) {
        ParamPreconditions.checkNotEmpty(id, CodeEnum.FAIL.getCode(), "没有传id");
        ywMemberRepository.delete(id);
        return Result.create("删除成功");
    }


    /**
     * 新的客户列表接口
     *
     * @param req
     * @return
     */
    @PostMapping(value = "query/old/order")
    @ApiDoc(Result.class)
    public Object findMemberOld(@RequestBody PaMemOldReq req) {
        log.info("会员列表查询req:{}", req);
        if (commonService.paginationSupport(req.getPageIndex(), req.getPageSize())) {
            return paMemberServiceNew.queryOrderOld(req);
        }
        return Result.create("ERROR", "少参数pageIndex或者pageSize");
    }
}
















